package com.zmzncs.lmtc.module.order.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.common.util.WeChatUtil;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.common.util.ExceptionUtil;
import com.zmzncs.lmtc.module.order.dao.ParkReserveRecordDao;
import com.zmzncs.lmtc.module.order.pojo.orderreserve.*;
import com.zmzncs.lmtc.module.order.service.ParkReserveRecordService;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 预约记录表 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-12-14
 */
@Service
public class ParkReserveRecordServiceImpl extends ServiceImpl<ParkReserveRecordDao, ParkReserveRecord> implements ParkReserveRecordService {

    @Autowired
    private ParkReserveRecordDao parkReserveRecordDao;
    @Autowired
    private ParkInfoDao parkInfoDao;
    @Autowired
    HttpServletRequest request;

    @Override
    public IPage<ParkReserveRecordDTO> page(ParkReserveRecordSearchForm form){
        IPage<ParkReserveRecordDTO> iPage = parkReserveRecordDao.list(form.getPage(), null);

        return iPage;
    }

    /**
     * 审核预约
     */
    @Override
    @Transactional
    public void auditReserve(Long id, Integer status){
        ParkReserveRecordDTO reserveRecordDTO = parkReserveRecordDao.getById(id);
        if(status.equals(2)){   //  通过
            ParkReserveRecord parkReserveRecord = new ParkReserveRecord();
            parkReserveRecord.setId(id);
            parkReserveRecord.setStatus(status);
            parkReserveRecordDao.updateById(parkReserveRecord);
        } else if (status.equals(3)){   //  驳回
            String tradeNo = WeChatUtil.generateTradeNo();

            Map result = WeChatUtil.refund(tradeNo, reserveRecordDTO.getReserveAmount(), reserveRecordDTO.getReserveAmount(), reserveRecordDTO.getTradeNo(), "预约超时");
            if(result != null && result.get("resultCode").equals("SUCCESS")){
                ParkReserveRecord parkReserveRecord = new ParkReserveRecord();
                parkReserveRecord.setId(reserveRecordDTO.getId());
                parkReserveRecord.setStatus(5);

                parkReserveRecordDao.updateById(parkReserveRecord);
            } else {
                ExceptionUtil.businessException("退款失败");
            }
        } else {
            ExceptionUtil.businessException("2：预约通过 3：预约驳回");
        }
    }

    @Override
    public void delete(Long id){
        ParkReserveRecord parkReserveRecord = new ParkReserveRecord();
        parkReserveRecord.setId(id);
//        parkReserveRecord.isDeleted(1);

        parkReserveRecordDao.updateById(parkReserveRecord);
    }

    @Override
    public void update(ParkReserveRecordUpdateForm form){
        ParkReserveRecord parkReserveRecord = new ParkReserveRecord();
        BeanUtils.copyProperties(form, parkReserveRecord);

        parkReserveRecordDao.updateById(parkReserveRecord);
    }

    @Override
    public ParkReserveRecordDTO getById(Long id){
        ParkReserveRecordDTO parkReserveRecordDTO = parkReserveRecordDao.getById(id);

        return parkReserveRecordDTO;
    }

    @Override
    public IPage<ParkReserveRecordDTO> getReserveRecord(Integer pageNum, Integer pageSize, Long parkId,Integer status) {

        Page page = new Page(pageNum, pageSize);
        IPage<ParkReserveRecordDTO> iPage = parkReserveRecordDao.getAllReserveRecord(page,parkId,status);
       if (status != null && status == 1){
           iPage = parkReserveRecordDao.getSubmitReserveRecord(page,parkId);

       }
        List<ParkReserveRecordDTO> records = iPage.getRecords();
        if (records != null && !records.isEmpty()){
            for (ParkReserveRecordDTO recordDTO : records) {

                recordDTO.setSubmitTime(recordDTO.getCreateTime());

                String beginTime = DateTimeUtil.convertYMDHMS(recordDTO.getReserveBeginTime());
                String endTime = DateTimeUtil.convertYMDHMS(recordDTO.getReserveEndTime());
                String reserveTime = beginTime.substring(0, 16) + "-" + endTime.substring(11,16);
                recordDTO.setReserveTime(reserveTime);
            }
        }
        return iPage;
    }

    /**
     * 昨天、今天、本周、本月的预约缴费
     */
    @Override
    public String getReserveAmountByDate(Long parkId, Integer dateType) {
        String reserveAmountByDate = parkReserveRecordDao.getReserveAmountByDate(parkId, dateType);
        return reserveAmountByDate;
    }


}
